<pre><code class="JavaScript"><xmp>mounted() {
    this.init()
    window.addEventListener('beforeunload',e => this.beforeunload(e))
    window.addEventListener('unload', e => this.unload(e))
  },

  destroyed(){
    window.removeEventListener('beforeunload',e => this.beforeunload(e))
    window.removeEventListener('unload', e => this.unload(e))
    this.exitRoom()
  },</xmp><xmp>  beforeunload(){
    this.beforeUnload_time=new Date().getTime()
  },
  unload(){
    this.gap_time=new Date().getTime()-this.beforeUnload_time;
    if(this.gap_time <= 5){
      console.log('关闭页面')
      this.exitRoom()
    }
  },<br/></xmp></code></pre>